#include<iostream>
using namespace std;
const int N = 510;
int a[N][N];
int n, m;
int st[N][N];
int main()
{
	cin >> n >> m;
	for (int i = 1; i <= m; i++) {
		for (int j = 1; j <= n; j++) {
			cin >> a[i][j];
		}
	}
	int ans = 0;
	for (int i = 1; i <= m; i++) {
		for (int j = 1; j <= n - 1; j++) {
			st[a[i][j]][a[i][j + 1]] = st[a[i][j + 1]][a[i][j]] = 1;
		}
	}
	for (int i = 1; i <= n - 1; i++) {
		for (int j = i + 1; j <= n; j++) {
			if (!st[a[1][i]][a[1][j]])ans++;
		}
	}
	//cout << st[1][4] << endl;
	cout << ans << endl;
	return 0;
}